/**
 * created  on 25/09/2017.
 */

const FormUtils = {
    /**
     *
     * @param isProtected Boolean
     */
    formInBoxDisabled: function (boxDiv, form, isProtected) {
        let submitBtn, cancelBtn, editorArray, editor;
        submitBtn = boxDiv.querySelector("div.modal-footer > button.cancel");
        cancelBtn = boxDiv.querySelector("div.modal-footer > button.submit");
        if (isProtected) {
            cancelBtn.setAttribute("disabled", "disabled");
            submitBtn.setAttribute("disabled", "disabled");

            cancelBtn.classList.add("disabled");
            submitBtn.classList.add("disabled");
        } else {
            cancelBtn.removeAttribute("disabled");
            submitBtn.removeAttribute("disabled");

            cancelBtn.classList.remove("disabled");
            submitBtn.classList.remove("disabled");
        }
        editorArray = form.elements;
        for (let i = 0; i < editorArray.length; i++) {
            editor = editorArray[i];
            if (isProtected) {
                editor.setAttribute("disabled", "disabled");
                editor.classList.add("disabled");
            } else {
                editor.removeAttribute("disabled");

                editor.classList.remove("disabled");
            }
        }

    },
    parseParam: function (form, params) {
        let editorArray;
        editorArray = form.querySelectorAll(".editor");
        for (let i = 0; i < editorArray.length; i++) {

            this._delegatingEditor_(editorArray[i], params);

        }
    }, _delegatingEditor_: function (editor, params) {
        let tag;
        tag = editor.tagName.toLowerCase();
        switch (tag) {
            case 'select':
                this._selectEditor_(editor, params);
                break;
            case 'input':
                this._delegatingInput_(editor, params);
                break;
            case 'textarea':
                params[editor.name] = editor.value.trim();
                break;
        }
    }, _selectEditor_: function (select, params) {
        if (select.multiple) {
            this.doSelectMultiple_(select, params);
        } else {
            params[select.name] = select.value;
        }
    }, doSelectMultiple_: function (select, params) {
        let options, suffix = "", counter;
        options = select.options;
        if (select.hasAttribute("data-name-suffix")) {
            suffix = "." + select.getAttribute("data-name-suffix");
        }
        counter = 0;
        for (let i = 0; i < options.length; i++) {
            let option;
            option = select.options[i];
            if (option.selected) {

                params[select.name + "[" + counter + "]" + suffix] = option.value;
                counter++;
            }

        }
    },
    _delegatingInput_: function (input, params) {
        let type;
        type = input.type;
        // 未实现完全,待有需求时再进行设计和实现
        switch (type) {
            case 'hidden':
            case 'text':
            case 'password':
            case 'radio':
            case 'checkbox':
            case 'color':
            case 'date':
            case 'datetime-local':
            case 'email':
            case 'number':
            case 'range':
            case 'search':
            case 'tel':
            case 'time':
            case 'url':
            case 'week':
            default:
                params[input.name] = input.value.trim();
        }
    }

};

const DataTableUtils = {
    /**
     * 根据选择行数决定 table 编辑按钮是否可见
     *1.onlyOne [class] 表示 只选择一行的情况下按钮可用
     * 2.leastOne [class] 表示 至少选择一行时按钮可用
     * 3.onlyOne 与 leastOne 不可同时使用
     * 4.noOne [class] 表示 没有行被选择时可用
     * @param dt dataTable 对象
     */
    decisionButtonVisible: function (dt) {
        let count, btnArray, btn, i;
        count = dt.rows({selected: true}).count();
        btnArray = dt.buttons().nodes();
        for (i = 0; i < btnArray.length; i++) {
            btn = btnArray[i];
            if (btn.classList.contains("leastOne")) {
                if (count > 0) {
                    btn.classList.remove("disabled");
                } else {
                    btn.classList.add("disabled");
                }
            } else if (btn.classList.contains("onlyOne")) {
                if (count === 1) {
                    btn.classList.remove("disabled");
                } else {
                    btn.classList.add("disabled");
                }
            } else if (btn.classList.contains("noOne")) {
                if (count === 0) {
                    btn.classList.remove("disabled");
                } else {
                    btn.classList.add("disabled");
                }
            }

        }
    }
};


const JsonMsgUtils = {

    init: function () {

    }, showSuccessMsg: function (data) {
        let type, message;
        type = typeof data;
        if (type === "object") {
            message = data.msg;
        } else {
            message = data;
        }
        $.notify({
            icon: 'glyphicon glyphicon-leaf',
            "title": "通知",
            message: message
        }, {
            type: "success",
            mouse_over: "pause",
            delay: 3000,
            placement: {
                align: "center"
            },
            animate: {
                enter: "animated",
                exit: "animated"
            }
        });
    }, showErrorMsg: function (data) {
        let type, message;
        type = typeof data;
        if (type === "object") {
            message = data.msg;
        } else {
            message = data;
        }
        BootstrapDialog.alert({
            "title": '错误',
            message: message,
            type: BootstrapDialog.TYPE_DANGER,
            closable: true,
            draggable: true,
            buttonLabel: '确认',
        });
    },
    /**
     * 从 json 结构{"code":"","msg":"","data":{}} 中解析出 消息
     * @param json
     * @param m true 表示行显示消息
     */
    getJsonErrorMsg: function (json, m) {
        let msg, row;
        row = m ? "<br/>" : "#";
        msg = json.code + "#" + json.msg;
        if (json.data) {
            msg += row + JSON.stringify(json.data);
        }
        return msg;
    },
    /**
     *
     * @param e 要显示的元素
     * @param json 结构{"code":"","msg":"","data":{}}
     */
    showMsgWithHelp: function (e, json) {
        let msg;
        msg = json.msg;
        switch (json.code) {
            case 'ok':
                e.classList.remove("text-warning");
                e.classList.remove("text-danger");
                e.classList.add("text-success");
                break;
            case 'dataValidError':
                e.classList.remove("text-danger");
                e.classList.remove("text-success");
                e.classList.add("text-warning");
                msg = JsonMsgUtils.getJsonErrorMsg(json, false);
                break;
            case 'dataError':
            default:
                e.classList.remove("text-warning");
                e.classList.remove("text-success");
                e.classList.add("text-danger");
                msg = JsonMsgUtils.getJsonErrorMsg(json, false);
        }
        e.innerHTML = msg;
    }


};